﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Common;
using System.ComponentModel;

namespace ResourceBlender.Core {
    /// <summary>
    /// A resource bundle
    /// </summary>
    public class RBBundle : INotifyPropertyChanged {
        private string applicationId, bundleId, name;

        /// <summary>
        /// Gets or sets the ID of the application which contains this bundle.
        /// </summary>
        /// <value>The application ID.</value>
        public string ApplicationID {
            get { return applicationId; }
            set {
                applicationId = value;
                OnPropertyChanged("ApplicationID");
            }
        }

        /// <summary>
        /// Gets or sets the bundle's ID.
        /// </summary>
        /// <value>The bundle ID.</value>
        public string BundleID {
            get { return bundleId; }
            set {
                bundleId = value;
                OnPropertyChanged("BundleID");
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name {
            get { return name; }
            set {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        public RBBundle() {
            applicationId = string.Empty;
            bundleId = Utils.GenID(true);
            name = string.Empty;
        }

        /// <summary>
        /// Initializes a new <see cref="RBBundle"/>.
        /// </summary>
        /// <param name="applicationId">The ID of the application which contains this bundle.</param>
        /// <param name="bundleId">The bundle ID.</param>
        /// <param name="name">The bundle name.</param>
        public RBBundle(string applicationId, string bundleId, string name) {
            this.ApplicationID = applicationId;
            this.BundleID = bundleId;
            this.Name = name;
        }

        /// <summary>
        /// Initializes a new <see cref="RBBundle"/>.
        /// </summary>
        /// <param name="reader">The reader containing all fields.</param>
        public RBBundle(DbDataReader reader) {
            if(reader.FieldCount < 3) {
                throw new ArgumentException("Reader must contain all fields");
            }
            this.ApplicationID = reader["application"].ToString();
            this.BundleID = reader["id"].ToString();
            this.Name = reader["name"].ToString();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string strPropertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }
    }
}